//
//  SearchViewController.swift
//  SkylowAPP
//
//  Created by 漫云 on 2024/9/22.
//

import UIKit

class SearchViewController: UIViewController,UISearchBarDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let searchBar = UISearchBar()
        searchBar.placeholder = "Search"
        searchBar.delegate = self as? UISearchBarDelegate
        searchBar.backgroundColor = .black
        //设置代理
        searchBar.delegate = self
        //设置搜索栏的颜色
        searchBar.barTintColor = UIColor.blue
        searchBar.tintColor = UIColor.white
        
        // 如果你想改变搜索框内的文本颜色
        if let textField = searchBar.value(forKey: "searchField") as? UITextField {
            textField.textColor = .white
        }
        

        self.navigationItem.titleView = searchBar
      
        setupUI()

    }
    //当输入文字变化时调用的方法
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        print(searchText)
    }
    
    // MARK: - homeView
    fileprivate func setupUI() {
        view.backgroundColor = UIColor.colorWith(hexString: "#333333")
                  
      let layout = SLWaterfallFlowLayout()
      layout.delegate = self
      let collection = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
    collection.backgroundColor =  UIColor.colorWith(hexString: "#333333")
      collection.delegate = self
      collection.dataSource = self
     let nibCell = UINib(nibName: "HomeItemCell", bundle: nil)
        collection.register(nibCell, forCellWithReuseIdentifier: HomeItemCell.identifiers)
        
      view.addSubview(collection)
    }
}
// MARK: - home
extension SearchViewController: UICollectionViewDelegate, UICollectionViewDataSource {
  func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 21
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HomeItemCell.identifiers, for: indexPath) as! HomeItemCell
    return cell
  }
}

extension SearchViewController: SLWaterfallFlowLayoutDelegate {
  func heightForRowAtIndexPath(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, indexPath: IndexPath, itemWidth: CGFloat) -> CGFloat {
      return CGFloat((arc4random() % 2 + 1) * 20 + 220)
  }
  
  func columnNumber(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> Int {
      return 2
  }
  
  func insetForSection(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
  }
  
  
  func lineSpacing(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> CGFloat {
    return 5
  }
  
  func interitemSpacing(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> CGFloat {
    return 5
  }
  
  func spacingWithLastSection(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> CGFloat {
      return 0
  }
}
